package com.hippo.database.config;

import com.alibaba.fastjson.support.spring.GenericFastJsonRedisSerializer;
import com.fasterxml.jackson.annotation.JsonAutoDetect;
import com.fasterxml.jackson.annotation.JsonTypeInfo;
import com.fasterxml.jackson.annotation.PropertyAccessor;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature;
import com.fasterxml.jackson.databind.jsontype.impl.LaissezFaireSubTypeValidator;
import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule;
import com.fasterxml.jackson.datatype.jsr310.deser.LocalDateDeserializer;
import com.fasterxml.jackson.datatype.jsr310.deser.LocalDateTimeDeserializer;
import com.fasterxml.jackson.datatype.jsr310.ser.LocalDateSerializer;
import com.fasterxml.jackson.datatype.jsr310.ser.LocalDateTimeSerializer;
import org.springframework.cache.annotation.EnableCaching;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.StringRedisSerializer;

import javax.swing.*;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.TimeZone;

/**
 * @Description: Redis配置类
 * @version 1.0.0
 * @auther tangxl
 * @Date 2023/4/25/025 21:38
 */
@Configuration
@EnableCaching // 开启缓存
public class RedisConfig {
    /**
     * 配置RedisTemplate序列化器和连接工厂
     * @param redisConnectionFactory
     * @return
     */
    @Bean
    public RedisTemplate<String, Object> redisTemplate(RedisConnectionFactory redisConnectionFactory) {
        // 创建RedisTemplate<String, Object>对象
        RedisTemplate<String, Object> template = new RedisTemplate<>();
        // 设置RedisTemplate的连接工厂
        template.setConnectionFactory(redisConnectionFactory);
        // json序列化配置，使用GenericFastJsonRedisSerializer替换默认jdk序列化器
        Jackson2JsonRedisSerializer jackson2JsonRedisSerializer = new Jackson2JsonRedisSerializer(Object.class);
        //时间序列化与反序列化问题
        JavaTimeModule timeModule = new JavaTimeModule();
        // 反序列化时间
        timeModule.addDeserializer(LocalDate.class,
                new LocalDateDeserializer(DateTimeFormatter.ofPattern("yyyy-MM-dd")));
        timeModule.addDeserializer(LocalDateTime.class,
                new LocalDateTimeDeserializer(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")));
        // 序列化时间
        timeModule.addSerializer(LocalDate.class,
                new LocalDateSerializer(DateTimeFormatter.ofPattern("yyyy-MM-dd")));
        timeModule.addSerializer(LocalDateTime.class,
                new LocalDateTimeSerializer(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")));
        // 创建ObjectMapper对象
        ObjectMapper om = new ObjectMapper();
        // 设置objectMapper的访问权限
        om.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);
        // 指定序列化输入类型,就是将数据库里的数据按照一定类型存储到redis缓存中。
        om.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL);
//        om.activateDefaultTyping(LaissezFaireSubTypeValidator.instance,ObjectMapper.DefaultTyping.NON_FINAL, JsonTypeInfo.As.WRAPPER_ARRAY);
        // 设置objectMapper的时间序列化器
        om.registerModule(timeModule);
        // 设置objectMapper的时区--中国上海
        om.setTimeZone(TimeZone.getTimeZone("Asia/Shanghai"));
        // 设置objectMapper的序列化器
        om.disable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS);
        jackson2JsonRedisSerializer.setObjectMapper(om);

        // 创建StringRedisSerializer序列化器
        StringRedisSerializer stringRedisSerializer = new StringRedisSerializer();
        // 设置key的序列化器
        template.setKeySerializer(stringRedisSerializer);
        // 设置value的序列化器
        template.setValueSerializer(jackson2JsonRedisSerializer);
        // 设置hash的key的序列化器
        template.setHashKeySerializer(stringRedisSerializer);
        // 设置hash的value的序列化器
        template.setHashValueSerializer(jackson2JsonRedisSerializer);
        // 设置支持事务
        template.setEnableTransactionSupport(true);
        // 设置hash的value的序列化器
        template.afterPropertiesSet();
        return template;
    }


}
